package bma.common.netty.thrift.dual;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.support.FileSystemXmlApplicationContext;

import bma.common.langutil.ai.stack.AIStackPrint;
import bma.common.langutil.ai.stack.AIStackROOT;
import bma.common.langutil.ai.stack.AIStackSimple;
import bma.common.langutil.core.ObjectUtil;
import bma.common.langutil.testcase.SpringTestcaseUtil;
import bma.common.netty.thrift.dual.sample.Hello4NTS;

public class NettyThriftDualClientTC {

	final org.slf4j.Logger log = org.slf4j.LoggerFactory
			.getLogger(NettyThriftDualClientTC.class);

	FileSystemXmlApplicationContext context;
	NettyThriftDualClient client;

	@Before
	public void setUp() throws Exception {
		SpringTestcaseUtil.disableDebug();
		context = new SpringTestcaseUtil.ApplicationContextBuilder().resource(
				NettyThriftDualClientTC.class, "netty-thrift-client.xml")
				.build();

		client = context.getBean("client", NettyThriftDualClient.class);
		System.out.println(client.toString());

		client.start();
	}

	@After
	public void tearDown() throws Exception {
		if (context != null)
			context.close();
	}

	@Test
	public void testStart() throws Exception {
		ObjectUtil.waitFor(this, 30 * 1000);
	}

	@Test
	public void testSample_name() throws Exception {
		AIStackROOT<NTSClient> stack = new AIStackROOT<NTSClient>() {

			@Override
			public boolean end(NTSClient result, Throwable t) {
				if (result != null) {
					Hello4NTS.Client cl = new Hello4NTS.Client(result, "hello");
					AIStackSimple<String> st = new AIStackSimple<String>(null);
					try {
						cl.name(st, "message from dual client");
						System.out.println(st.get());
					} catch (Exception e) {
						t = e;
					}
				}
				if (t != null) {
					log.error("call fail", t);
				}
				return true;
			}
		};
		client.getClient(stack, 5000);
		ObjectUtil.waitFor(this, 30 * 1000);
	}

	@Test
	public void testSample_asyn() throws Exception {
		AIStackROOT<NTSClient> stack = new AIStackROOT<NTSClient>() {

			@Override
			public boolean end(NTSClient result, Throwable t) {
				if (result != null) {
					Hello4NTS.Client cl = new Hello4NTS.Client(result, "hello");
					try {
						AIStackPrint<Boolean> st1 = new AIStackPrint<Boolean>(
								"sleep");
						cl.sleep(st1, 100);

						AIStackPrint<String> st2 = new AIStackPrint<String>(
								"name");
						cl.name(st2, "message from dual client");
					} catch (Exception e) {
						t = e;
					}
				}
				if (t != null) {
					log.error("call fail", t);
				}
				return true;
			}
		};
		client.getClient(stack, 5000);
		ObjectUtil.waitFor(this, 30 * 1000);
	}

	@Test
	public void testSample_sayPushback() throws Exception {
		AIStackROOT<NTSClient> stack = new AIStackROOT<NTSClient>() {

			@Override
			public boolean end(NTSClient result, Throwable t) {
				if (result != null) {
					Hello4NTS.Client cl = new Hello4NTS.Client(result, "hello");
					AIStackPrint<Boolean> st = new AIStackPrint<Boolean>("say");
					try {
						cl.say(st, "message from dual client");
					} catch (Exception e) {
						t = e;
					}
				}
				if (t != null) {
					log.error("call fail", t);
				}
				return true;
			}
		};
		client.getClient(stack, 5000);
		ObjectUtil.waitFor(this, 30 * 1000);
	}

}
